package algorithm.stream;

import algorithm.linear.PrimeRing;

import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Stream API操作
 */
public class StreamLearn {
    public static void main(String[] args) {
        //randomOpt();
        //selfSupplier();
        Stream.iterate(0,n->n+3).limit(10).forEach(x-> System.out.println(x));
    }

    public static void randomOpt() {
        Random seed = new Random();
        Supplier<Integer> random = seed::nextInt;
        Stream.generate(random).limit(10).forEach(System.out::println);

        IntStream.generate(()->(int)System.nanoTime()%100).limit(10).forEach(System.out::println);
    }

    public static void selfSupplier() {
        Stream.generate(new PersonSupplier())
                .limit(10)
                .forEach(
                        p-> System.out.println( p.getName()+", "+p.getAge() )
                );
    }
    private static class PersonSupplier implements Supplier<Person>{
        private int index = 0;
        private Random random = new Random();
        @Override
        public Person get() {
            return new Person(index++,"STU"+index,random.nextInt(100));
        }
    }

    private static class Person{
        public int no;
        private String name;
        private int age;



        public Person(int no, String name,int age){
            this.no = no;
            this.name = name;
            this.age = age;
        }
        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
        public int getNo() {
            return no;
        }

        public void setNo(int no) {
            this.no = no;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
